var selectorStockIDs = []; //选中的id
$(function(){
	$("#selectorJSDateShow").html(dateTimeFormat("yyyy-MM-dd",new Date()));
	//请求数据
	createMainTable();
	//翻页
	var pageIndex = 0;
	$('#main').on('tap', '.pre', function(){
		//上一页
		pageIndex--;
		createMainTable({
			pageIndex: pageIndex
		});
	}).on('tap', '.next', function(){
		//下一页
		pageIndex++;
		createMainTable({
			pageIndex: pageIndex
		});
	});


	//搜索确认按钮
	$("#confirmBtn").click(function(){
		createMainTable(); //请求数据
		mui.back(); //mui 返回
	});


	//选择器
	$(".myPicker").click(function(){
		var self = this;
		var nowDate = new Date();
		//创建普通的popPicker
		var popPicker = new mui.PopPicker();
		var popDataArray = new Array();
		//创建日期形式的Depicker
		var datePicker = new mui.DtPicker({
			type: "date",
			endDate: nowDate
		});
		switch ($(self).attr('id')){
			//结算日期选择器
			case 'selectorJSDate':
				datePicker.setSelectedValue(getMonthStartDate());
				datePicker.show(function(items) {
					$("#selectorJSDateShow").html(items.value);
					datePicker.dispose();
				});
				break;
			//开始日期选择器
			case 'selectorBeginDate':
				datePicker.setSelectedValue(getMonthStartDate());
				datePicker.show(function(items) {
					$("#selectorBeginDateShow").html(items.value);
					datePicker.dispose();
				});
				break;
			//结束日期选择器
			case 'selectorEndDate':
				datePicker.show(function(items) {
					$("#selectorEndDateShow").html(items.value);
					datePicker.dispose();
				});
				break;
			//清算状态
			case 'selectorClear':
				popDataArray = [];//将数组清空
				popDataArray = [{value: '' ,text: '全部'},{value: '1' ,text: '已审核'},{value: '0' ,text: '未审核'}];
				popPicker.setData(popDataArray);
				popPicker.show(function(items) {
					$("#selectorClearShow").html(items[0].text).attr('data-value',items[0].value);
					popPicker.dispose();
				});
				break;
			default:
				break;
		}
	});

	//checkBox
	//全选
	// $(".checkAll").click(function(){
	//
	// });
	// //单选
	// $(".checkOne").click(function(){
	//
	// });
})

//全选
function checkAll(that){
	selectorStockIDs = [];
	var isClickAll = that.checked;
	$(".checkOne").each(function(){
		this.checked = isClickAll;
		if(this.checked == true){
			selectorStockIDs.push($(this).parents('td').attr('data-id'));
		}
	});
}
//单选
function checkOne(){
	var checkBox = $(".checkOne:checked");
	$(".checkAll").prop("checked",($(".checkOne").length === checkBox.length));
	selectorStockIDs = [];
	checkBox.each(function(){
		selectorStockIDs.push($(this).parents('td').attr('data-id'));
	});
}

//获取数据
function createMainTable(params){
	params = params || {};
	//better 这样写更好
	var data = {
		qrySubject: $('#qrySubjectS').val(),
		qryRes: $('#qryResS').val(),
		qryMainCreateName: $('#qryMainCreateNameS').val(),
		qryWare: $("#qryWareS").val(),
		qryResState: $("#qryResStateS").val(),
		qryDateB: $('#selectorBeginDateShow').html(),
		qryDateE: $('#selectorEndDateShow').html()
	};
	//显示当前查询条件
	$("#qryRes").html(data.qryRes);
	$("#qrySubject").html(data.qrySubject);
	$("#qryMainCreateName").html(data.qryMainCreateName);
	$("#qryResState").html(data.qryResState);
	$("#qryWare").html(data.qryWare);
	$("#beginDate").html(data.qryDateB);
	$("#endDate").html(data.qryDateE);

	$('.no-data').html('<span class="mui-spinner"></span>')
	var pageNo = params.pageIndex || 0;
	Utils.request(Utils.Api.ioJsGet,  $.extend({
		start: 10*pageNo,
		limit: 10,
		pageIndex: pageNo,
		field: 'accMainNo',
		direction: 'DESC',
	},data), function(data){
		$('#table').html(template('tableTem', data.rows));
		$('#total').html(data.results);
		var pageHtml = $('#main');
		pageHtml.find('button').removeAttr('disabled');
		if(data.rows.length < 10){
			pageHtml.find('.next').attr('disabled', 'disabled')
		}
		if(pageNo == 0){
			pageHtml.find('.pre').attr('disabled', 'disabled')
		}
	})
}

//生成结算单
function examine(){
	if(selectorStockIDs.length === 0){
		mui.alert('还没有选中记录');
		return;
	}
	mui.confirm('确认结算选中的'+selectorStockIDs.length+'条记录吗？', '结算', ['取消','确认'], function(e) {
		if (e.index == 1) {
			//确认
			Utils.request(
				Utils.Api.ioJsCreate,
				{
					ids: selectorStockIDs,
					jsDate: $('#selectorJSDateShow').html(),
					jsRemark: $('#jsRemark').val()
				},
				function(){
					mui.alert('结算成功')
					createMainTable()
				}
			)
		} else {
			//取消
		}
	})
}
